{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名 王珂  \n",
    "+ 学号 0163446"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你一直在我看不见的地方\n",
      "指引着前进方向 为我保驾护航\n",
      "一路有你相伴 迷雾也随之消散\n",
      "是天使还是恶魔 我都不会有遗憾\n",
      "早就领教过 着世间没有绝对\n",
      "流星会有交汇 生命有笑也有累\n",
      "原以为 毕竟你我不是谁的谁\n",
      "却难料这双雄 竟然会是绝配\n",
      "像迷雾 像星光 像对立的镜像\n",
      "真实或虚妄\n",
      "转瞬间欲盖弥彰 隐藏贪婪和嚣张\n",
      "谁的梦 谁的伤 谁遗落的勋章\n",
      "无谓的倔强\n",
      "倒影中追述过往 多庆幸你在身旁\n",
      "只要你在身旁\n",
      "\n",
      "你总悄声无息 风暴却是潜移默化\n",
      "镜中的你是你是我自己\n",
      "总在决裂后反悔 沉默后又破功\n",
      "但是心照不宣下的承诺 我们都懂\n",
      "要追捕真相 追捕神的目光\n",
      "追捕心里最后一次对生存的信仰\n",
      "是与否 想要的 到底怎么得手\n",
      "在下一个路口 你我是敌是友\n",
      "像迷雾 像星光 像对立的镜像\n",
      "真实或虚妄\n",
      "转瞬间欲盖弥彰 隐藏贪婪和嚣张\n",
      "谁的梦 谁的伤 谁遗落的勋章\n",
      "无谓的倔强\n",
      "倒影中追述过往 多庆幸你在身旁\n",
      "只要你在身旁\n",
      "\n",
      "像迷雾 像星光 像对立的镜像\n",
      "真实或虚妄\n",
      "转瞬间欲盖弥彰 隐藏贪婪和嚣张\n",
      "谁的梦 谁的伤 谁遗落的勋章\n",
      "无谓的倔强\n",
      "倒影中追述过往 多庆幸你在身旁\n",
      "只要你在身旁\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def refrain():\n",
    "    print(\"像迷雾 像星光 像对立的镜像\\n真实或虚妄\\n转瞬间欲盖弥彰 隐藏贪婪和嚣张\\n谁的梦 谁的伤 谁遗落的勋章\\n无谓的倔强\\n倒影中追述过往 多庆幸你在身旁\\n只要你在身旁\\n\")\n",
    "print(\"你一直在我看不见的地方\\n指引着前进方向 为我保驾护航\\n一路有你相伴 迷雾也随之消散\\n是天使还是恶魔 我都不会有遗憾\\n早就领教过 着世间没有绝对\\n流星会有交汇 生命有笑也有累\\n原以为 毕竟你我不是谁的谁\\n却难料这双雄 竟然会是绝配\")\n",
    "refrain()\n",
    "print(\"你总悄声无息 风暴却是潜移默化\\n镜中的你是你是我自己\\n总在决裂后反悔 沉默后又破功\\n但是心照不宣下的承诺 我们都懂\\n要追捕真相 追捕神的目光\\n追捕心里最后一次对生存的信仰\\n是与否 想要的 到底怎么得手\\n在下一个路口 你我是敌是友\")\n",
    "refrain()\n",
    "refrain()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有12个数\n",
      "样本均值为6.5\n",
      "样本方差为13.0\n",
      "一共有14个数\n",
      "样本均值为7.5\n",
      "样本方差为17.5\n"
     ]
    }
   ],
   "source": [
    "def calculating(*arg):\n",
    "    n=0\n",
    "    its_sum=0\n",
    "    for i in arg:\n",
    "        its_sum+=i\n",
    "        n+=1\n",
    "        mean=its_sum/n\n",
    "    print('一共有%s个数'%n)\n",
    "    print('样本均值为%s'%mean)\n",
    "    sum_of_square=0\n",
    "    for i in arg:\n",
    "        sum_of_square+=(i-mean)**2\n",
    "        variance=sum_of_square/n\n",
    "        sample_variance=n*variance/(n-1)\n",
    "    print('样本方差为%s'%sample_variance)\n",
    "calculating(1,2,3,4,5,6,7,8,9,10,11,12)\n",
    "calculating(1,2,3,4,5,6,7,8,9,10,11,12,13,14)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)\n",
      "一共有12个数\n",
      "样本均值为6.5\n",
      "样本标准差为3.605551275463989\n",
      "\n",
      "number (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)\n",
      "一共有14个数\n",
      "样本均值为7.5\n",
      "样本方差为16.25\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def calculating(*number,var=None,std=None):\n",
    "    n=0\n",
    "    its_sum=0\n",
    "    for i in number:\n",
    "        its_sum+=i\n",
    "        n+=1\n",
    "        mean=its_sum/n\n",
    "    print('number',number)\n",
    "    print('一共有%s个数'%n)\n",
    "    print('样本均值为%s'%mean)\n",
    "    sum_of_square=0\n",
    "    for i in number:\n",
    "        sum_of_square+=(i-mean)**2\n",
    "        variance=sum_of_square/n\n",
    "        sample_variance=n*variance/(n-1)\n",
    "        sample_standard_variance=sample_variance**0.5\n",
    "    if var:\n",
    "        print('样本方差为%s\\n'%variance)\n",
    "    if std:\n",
    "        print('样本标准差为%s\\n'%sample_standard_variance)\n",
    "calculating(1,2,3,4,5,6,7,8,9,10,11,12,std=1) \n",
    "calculating(1,2,3,4,5,6,7,8,9,10,11,12,13,14,var=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number (14, 2, 8, 4, 5, 1, 7, 8, 3, 10, 11, 12)\n",
      "一共有12个数\n",
      "样本均值为7.083333333333333\n",
      "样本方差为15.909722222222221\n",
      "样本标准误为1.2026380934588692\n",
      "最小值为1\n",
      "number (7, 2, 0, 4, 5, 6, 7, 8, 19, 10, 11, 5, 13, 14)\n",
      "一共有14个数\n",
      "样本均值为7.928571428571429\n",
      "样本标准差为5.075799076375685\n",
      "样本标准误为1.3565643648501022\n",
      "最大值为19\n",
      "number (2, 5, 4, 7, 11)\n",
      "一共有5个数\n",
      "样本均值为5.8\n",
      "最大值为11\n",
      "最小值为2\n",
      "number (1, 5, 7, 9, 31, 22, 15)\n",
      "一共有7个数\n",
      "样本均值为12.857142857142858\n",
      "样本标准误为3.9906352962939633\n",
      "最大值为31\n",
      "最小值为1\n"
     ]
    }
   ],
   "source": [
    "def calculating(*number,var=None,std=None,se=None,maximum=None,minimum=None):\n",
    "    n=0\n",
    "    its_sum=0\n",
    "    for i in number:\n",
    "        its_sum+=i\n",
    "        n+=1\n",
    "        mean=its_sum/n\n",
    "    print('number',number)\n",
    "    print('一共有%s个数'%n)\n",
    "    print('样本均值为%s'%mean)\n",
    "    sum_of_square=0\n",
    "    for i in number:\n",
    "        sum_of_square+=(i-mean)**2\n",
    "        variance=sum_of_square/n\n",
    "        sample_variance=n*variance/(n-1)\n",
    "        sample_standard_variance=sample_variance**0.5\n",
    "        standard_error=sample_standard_variance/(n**0.5)\n",
    "        its_max=max(number)\n",
    "        its_min=min(number)\n",
    "    if var:\n",
    "        print('样本方差为%s'%variance)\n",
    "    if std:\n",
    "        print('样本标准差为%s'%sample_standard_variance)\n",
    "    if se:\n",
    "        print('样本标准误为%s'%standard_error)\n",
    "    if maximum:\n",
    "        print('最大值为%s'%its_max)\n",
    "    if minimum:\n",
    "        print('最小值为%s'%its_min)\n",
    "calculating(14,2,8,4,5,1,7,8,3,10,11,12,var=1,se=1,minimum=1) \n",
    "calculating(7,2,0,4,5,6,7,8,19,10,11,5,13,14,std=1,se=1,maximum=1)\n",
    "calculating(2,5,4,7,11,minimum=1,maximum=1)\n",
    "calculating(1,5,7,9,31,22,15,se=1,minimum=1,maximum=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
